/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatTableGUI.java
 *
 * Created on 30/08/2011, 01:01:36 AM
 */
package messgui;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import messengerclient.Controller;
import messengerclient.observers.ObsNew;
import messengercommon.dto.Message;
import messengercommon.person.Contact;
import messgui.classes.ContactList;
import messgui.classes.StatusBar;

/**
 *
 * @author Samus
 */
public class ChatTableGUI extends javax.swing.JFrame implements ObsNew, StatusBar {

    private ContactList contactlist;

    /** Creates new form ChatTableGUI */
    public ChatTableGUI() throws RemoteException {
        this.contactlist = new ContactList();
        initComponents();
        Controller.getInstance().assignTable(contactlist);
        Controller.getInstance().assignChatController(this);
        Controller.getInstance().contactChange();
        Txt_User.setText(Controller.getInstance().getPersonName());
        Txt_User.repaint();
        Txt_Nick.setText(Controller.getInstance().getNick());
        Txt_Nick.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Pnl_Chat = new javax.swing.JPanel();
        Btn_Chat = new javax.swing.JButton();
        Btn_Logout = new javax.swing.JButton();
        Btn_ACont = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        Txt_Contact = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        Lst_Contacts = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        Txt_Status = new javax.swing.JTextPane();
        jLabel2 = new javax.swing.JLabel();
        Txt_User = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        Btn_Nick = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        Txt_Nick = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        Pnl_Chat.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        Pnl_Chat.setName("Pnl_Chat"); // NOI18N

        Btn_Chat.setText("Chat...");
        Btn_Chat.setName("Btn_Chat"); // NOI18N
        Btn_Chat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Btn_ChatMouseClicked(evt);
            }
        });

        Btn_Logout.setText("Logout");
        Btn_Logout.setName("Btn_Logout"); // NOI18N
        Btn_Logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Btn_LogoutActionPerformed(evt);
            }
        });

        Btn_ACont.setText("Agregar Contacto");
        Btn_ACont.setName("Btn_ACont"); // NOI18N
        Btn_ACont.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Btn_AContMouseClicked(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        Txt_Contact.setName("Txt_Contact"); // NOI18N
        jScrollPane1.setViewportView(Txt_Contact);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        Lst_Contacts.setModel(this.contactlist);
        Lst_Contacts.setName("Lst_Contacts"); // NOI18N
        jScrollPane2.setViewportView(Lst_Contacts);

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        Txt_Status.setEnabled(false);
        Txt_Status.setName("Txt_Status"); // NOI18N
        jScrollPane4.setViewportView(Txt_Status);

        jLabel2.setText("Usuario: ");
        jLabel2.setName("jLabel2"); // NOI18N

        Txt_User.setFont(new java.awt.Font("Bookman Old Style", 0, 12));
        Txt_User.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        Txt_User.setText("----X----");
        Txt_User.setName("Txt_User"); // NOI18N

        jLabel4.setText("Nick:");
        jLabel4.setName("jLabel4"); // NOI18N

        Btn_Nick.setText("Cambiar");
        Btn_Nick.setName("Btn_Nick"); // NOI18N
        Btn_Nick.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Btn_NickMouseClicked(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        Txt_Nick.setEnabled(false);
        Txt_Nick.setName("Txt_Nick"); // NOI18N
        Txt_Nick.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Txt_NickKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(Txt_Nick);

        javax.swing.GroupLayout Pnl_ChatLayout = new javax.swing.GroupLayout(Pnl_Chat);
        Pnl_Chat.setLayout(Pnl_ChatLayout);
        Pnl_ChatLayout.setHorizontalGroup(
            Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Pnl_ChatLayout.createSequentialGroup()
                .addGroup(Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(Pnl_ChatLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(Btn_Chat)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 127, Short.MAX_VALUE)
                        .addComponent(Btn_Logout)
                        .addGap(20, 20, 20))
                    .addGroup(Pnl_ChatLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Btn_ACont)))
                .addContainerGap(20, Short.MAX_VALUE))
            .addGroup(Pnl_ChatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
            .addGroup(Pnl_ChatLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, Pnl_ChatLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(Btn_Nick))
                    .addGroup(Pnl_ChatLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(60, 60, 60)
                        .addComponent(Txt_User)))
                .addContainerGap())
        );
        Pnl_ChatLayout.setVerticalGroup(
            Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Pnl_ChatLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Txt_User)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Btn_Nick)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Btn_Chat)
                    .addComponent(Btn_Logout))
                .addGap(12, 12, 12)
                .addGroup(Pnl_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Btn_ACont))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jLabel1.setFont(new java.awt.Font("Bookman Old Style", 0, 18));
        jLabel1.setText("Chat Window");
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Pnl_Chat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Pnl_Chat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void Btn_LogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Btn_LogoutActionPerformed
    Controller.getInstance().userDisconnect();
    System.exit(0);
}//GEN-LAST:event_Btn_LogoutActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    Controller.getInstance().userDisconnect();
}//GEN-LAST:event_formWindowClosed

private void Btn_ChatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Btn_ChatMouseClicked
    Contact contact = (Contact) Lst_Contacts.getSelectedValue();
    ChatGUI chat = new ChatGUI(contact);
    Controller.getInstance().addObsList(contact.getUsername(), chat);
    chat.setVisible(true);
}//GEN-LAST:event_Btn_ChatMouseClicked
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    Controller.getInstance().userDisconnect();
}//GEN-LAST:event_formWindowClosing

private void Btn_AContMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Btn_AContMouseClicked
    try {
        int res = Controller.getInstance().addContact(Txt_Contact.getText());
        if (res == 0) {
            changeStatus("No existe ese contacto");
        } else if (res == 1) {
            changeStatus("Ese contacto ya está en la lista");
        } else {
            changeStatus("Contacto ingresado correctamente");
            Txt_Contact.setText("");
            Txt_Contact.repaint();
            Controller.getInstance().contactChange();
        }
    } catch (RemoteException ex) {
        Logger.getLogger(ChatTableGUI.class.getName()).log(Level.SEVERE, null, ex);
    }

}//GEN-LAST:event_Btn_AContMouseClicked

private void Btn_NickMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Btn_NickMouseClicked
    if (Txt_Nick.isEnabled()) {
        try {
            Controller.getInstance().setNewNick(Txt_Nick.getText());
            Txt_Nick.setEnabled(false);
        } catch (RemoteException ex) {
            changeStatus("Error al conectar con el cliente.");
        }
    } else {
        Txt_Nick.setEnabled(true);
        Btn_Nick.setEnabled(false);
    }
}//GEN-LAST:event_Btn_NickMouseClicked

private void Txt_NickKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Txt_NickKeyPressed
    if (Txt_Nick.getText() != null) {
        Btn_Nick.setEnabled(true);
    } else {
        Btn_Nick.setEnabled(false);
    }
}//GEN-LAST:event_Txt_NickKeyPressed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ChatTableGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ChatTableGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ChatTableGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ChatTableGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new ChatTableGUI().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Btn_ACont;
    private javax.swing.JButton Btn_Chat;
    private javax.swing.JButton Btn_Logout;
    private javax.swing.JButton Btn_Nick;
    private javax.swing.JList Lst_Contacts;
    private javax.swing.JPanel Pnl_Chat;
    private javax.swing.JTextPane Txt_Contact;
    private javax.swing.JTextPane Txt_Nick;
    private javax.swing.JTextPane Txt_Status;
    private javax.swing.JLabel Txt_User;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    // End of variables declaration//GEN-END:variables

    public ContactList getContactlist() {
        return contactlist;
    }

    public void setContactlist(ContactList contactlist) {
        this.contactlist = contactlist;
    }

    public void newWindow(Message message) {
        Contact contact = Controller.getInstance().darContacto(message.getOrigen());
        ChatGUI chat = new ChatGUI(contact);
        Controller.getInstance().addObsList(contact.getUsername(), chat);
        chat.setVisible(true);
        chat.receiveMessage(message);
    }

    public void changeStatus(String status) {
        Txt_Status.setText(status);
        Txt_Status.repaint();
    }

    public void contactChange() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
